package com.zmc.dynamic.service.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.zmc.dynamic.common.exception.ResourceNotFoundException;
import com.zmc.dynamic.domain.Student;
import com.zmc.dynamic.domain.User;
import com.zmc.dynamic.service.StudentService;
import com.zmc.dynamic.service.TransService;
import com.zmc.dynamic.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TransServiceImpl implements TransService {

    @Autowired
    private UserService userService;
    @Autowired
    private StudentService studentService;


    @Override
    @DSTransactional //xa模式：两阶段提交，若在提交阶段一个成功一个异常，则无法回滚(这种情况用seata处理)
    public void update() {
        User user = userService.getByUserId(1L);
        user.setVersion((int)Math.random() * 100);
        user.setVersion(8);
        user.setRealName("guagua");
        user.setNickName("lalala");
        userService.updateUser(user);
        Student student = studentService.getByStudentId(6L);
        student.setCore(7);
        student.setName("honhon");
        studentService.updateStudent(student);
        throw new ResourceNotFoundException();
    }
}
